﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using cart.model;
using cart.utils;
using System.Data.SqlClient;
using System.Collections;

namespace cart.dao
{
    public class cartDao
    {
        /// <summary>
        /// 将一个商品添加到购物车
        /// ousinka 2009年12月16日19:36:12
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        public Boolean addCart(cartModel cm)
        {
            string INSERT_INTO_CART = "insert into cart values(@userName,@productID,@productNumber,@addTime)";
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@userName",cm.userName),
                new SqlParameter("@productID",cm.productID),
                new SqlParameter("@productNumber",cm.productNumber),
                new SqlParameter("@addTime",cm.addTime)
            };
           return DB.executeNonQuerySQL(INSERT_INTO_CART,parm);
        }

        /// <summary>
        /// 根据用户名得到购物车
        /// ousinka 2009年12月16日19:39:29
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public ArrayList getCartByUserNameOrProductID(string userName)
        {
            string SELECT_CART_BY_NAME = "select * from cart where userID= '" + userName.ToString()+"'";
            return getCart(SELECT_CART_BY_NAME);
        }

        /// <summary>
        /// 根据用户名和商品id得到购物车
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        public ArrayList getCartByUserNameOrProductID(string userName, int productID)
        {
            string SELECT_CART_BY_USERNAME_PRODUCTID = "select * from cart where userID='" + userName + "' and productID= " + productID;
            return getCart(SELECT_CART_BY_USERNAME_PRODUCTID);
        }

        /// <summary>
        /// 更新购物车里的商品
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        public Boolean updateCartItem(cartModel cm)
        {
            string UPDATE_CART_BY_USERNAME_PRODUCTID = "update cart set productNumber=@pNumber where userID=@username and productID=@productID";
            SqlParameter[] parms = new SqlParameter[] { 
                new SqlParameter("@pNumber",cm.productNumber),
                new SqlParameter("@username",cm.userName),
                new SqlParameter("@productID",cm.productID)                
            };
            return DB.executeNonQuerySQL(UPDATE_CART_BY_USERNAME_PRODUCTID,parms);
        }

        /// <summary>
        /// 删除购物车里的一条记录
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        public Boolean deleteCartItem(cartModel cm)
        {
            string DELETE_CART_ITME = "delete cart where userID=@username and productID=@productID";
            SqlParameter[] parms = new SqlParameter[] 
            {
                new SqlParameter("@username",cm.userName),
                new SqlParameter("@productID",cm.productID)
            };
            return DB.executeNonQuerySQL(DELETE_CART_ITME,parms);
        }


        public Boolean clearCart(String userID)
        {
            string DELETE_CLEAR_CART = "delete cart where userID='" + userID + "'";
            return DB.executeNonQuerySQL(DELETE_CLEAR_CART);
        }

        /// <summary>
        /// 根据sql语句得到购物车
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        private ArrayList getCart(string sql)
        {
            SqlConnection conn = DB.getSqlConnection();
            DB.connectionOpen(conn);
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader sdr = cmd.ExecuteReader();
            ArrayList cartLists = new ArrayList();
            while (sdr.Read())
            {
                cartModel cm = new cartModel();
                cm.userName = sdr.GetString(0);
                cm.productID = sdr.GetInt32(1);
                cm.productNumber = sdr.GetInt32(2);
                cm.addTime = sdr.GetDateTime(3);
                cartLists.Add(cm);
            }
            DB.connectionClose(conn);
            return cartLists;
        }
    } 
}
